﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using DevComponents.AdvTree;
using NanoERP.Model;
using System.Collections.Generic;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    [Module("FrmStorageinfoList")]
    public partial class FrmStorageinfoList : ModuleForm
    {
        readonly SysdictableBLL _sysdictablebLl;
        readonly StorageinfoBLL _storageinfoBLL;
        public FrmStorageinfoList()
        {
            InitializeComponent();

            _storageinfoBLL = new StorageinfoBLL();
            _sysdictablebLl = new SysdictableBLL();

            BindTreeView();
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryInfo()
        {
            var node = dicTree.SelectedNode;
            if (node == null) return;

            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _storageinfoBLL.Query(int.Parse(node.Name), txtKey.Text.Trim());
        }

        /// <summary>
        /// 绑定仓库类别树
        /// </summary>
        void BindTreeView()
        {
            dicTree.Nodes.Clear();

            var sysdic = _sysdictablebLl.Get((int)SysDicEnum.仓库类别);
            var firNode = new Node(sysdic.Dicname) { Tag = sysdic, Name = sysdic.Id.ToString(), Image = NanoERP.Properties.Resources.XmlStructure };
            firNode.ExpandAll();
            var firList = _sysdictablebLl.Query(sysdic.Id);
            RecursiveLoopNodes(firList, firNode);

            dicTree.Nodes.Add(firNode);
            if (dicTree.Nodes.Count > 0) 
                dicTree.SelectedNode = dicTree.Nodes[0].Nodes.Count > 0 ? dicTree.Nodes[0].Nodes[0] : dicTree.Nodes[0];
        }

        /// <summary>
        /// 递归循环调用
        /// </summary>
        /// <param name="firList">一级树集合</param>
        /// <param name="parentNode">上级节点</param>
        void RecursiveLoopNodes(List<Sysdictable> firList, Node parentNode)
        {
            firList.ForEach(r =>
            {
                var secNode = new Node(r.Dicname) { Tag = r, Name = r.Id.ToString(), Image = NanoERP.Properties.Resources.CreateTableTemplatesGallery };

                parentNode.Nodes.Add(secNode);
                var recList = _sysdictablebLl.Query(r.Id);
                if (recList.Count > 0)
                    RecursiveLoopNodes(recList, secNode);
            });
        }

        private void tsbSelectType_Click(object sender, EventArgs e)
        {
            var f = new FrmDicData((int)SysDicEnum.仓库类别, true, true);
            f.ShowDialog(this);

            BindTreeView();
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            var obj = dicTree.SelectedNode.Tag as Sysdictable;
            var f = new FrmStorageinfoEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;
            var id = ((Storageinfo)dgvList.CurrentRow.DataBoundItem).Id;

            var obj = _storageinfoBLL.Get(id);
            var sysdic = dicTree.SelectedNode.Tag as Sysdictable;
            var f = new FrmStorageinfoEdit(obj, sysdic);
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;
            var obj = dgvList.CurrentRow.DataBoundItem as Storageinfo;
            var text = string.Format(@"您要确定要“删除”当前仓库“{0}”吗？", obj.Stoname);
            if (MessageBox.Show(text, "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _storageinfoBLL.Delete(obj.Id);
                QueryInfo();
            }
        }

        private void tsbPrint_Click(object sender, EventArgs e)
        {

        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            string msgLog;
            ExcelUtil.ExportToExcel(dgvList, "分支机构信息", out msgLog);
            if (msgLog != null)
                MessageBox.Show(msgLog, @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryInfo();
        }

        private void dicTree_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            QueryInfo();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            tsbEdit_Click(null, null);
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ControlUtil.DataGridRowPostPaint(sender, e);
        }
    }
}
